package com.ww.editsms.widget;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.chinawangw.R;

/**
 * @since 
 * @version 2013-6-28
 * @author wangwei
 */
public class SearchEditText extends EditText {
	private Drawable dRight;
	Context context;
	private Rect rBounds;
	public PopupWindow popupWindow;
	private boolean show = false;

	public SearchEditText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.context = context;
		checkOpen();
		initEditText();
	}

	public SearchEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		checkOpen();
		this.context = context;
		initEditText();
	}

	public SearchEditText(Context context) {
		super(context);
		checkOpen();
		this.context = context;
		initEditText();
	}

	private void checkOpen() {
		if (getTag() != null) {
			String str = (String) getTag();
			if ("open".equals(str)) {
				show = true;
			}
		}
	}

	@Override
	public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) {
		if (right != null) {
			dRight = right;
		}
		super.setCompoundDrawables(left, top, right, bottom);
	}

	public Drawable getRightDrawable() {
		return dRight;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if ((this.dRight != null) && (event.getAction() == 1)) {
			this.rBounds = this.dRight.getBounds();
			int i = (int) event.getX();
			if (i > getRight() - 3 * this.rBounds.width()) {
				setText("");
				open();
				event.setAction(MotionEvent.ACTION_CANCEL);
			}
		}

		return super.onTouchEvent(event);
	}

	@Override
	protected void finalize() throws Throwable {
		dRight = null;
		rBounds = null;
		super.finalize();
	}

	// 初始化edittext 控件
	private void initEditText() {
		setEditTextDrawable();
		addTextChangedListener(new TextWatcher() { // 对文本内容改变进行监听
			public void afterTextChanged(Editable paramEditable) {
			}

			public void beforeTextChanged(CharSequence paramCharSequence, int paramInt1, int paramInt2, int paramInt3) {
			}

			public void onTextChanged(CharSequence paramCharSequence, int paramInt1, int paramInt2, int paramInt3) {
				SearchEditText.this.setEditTextDrawable();
			}
		});
	}

	// 控制图片的显示
	private void setEditTextDrawable() {
		if (show) {
			setCompoundDrawables(null, null, this.dRight, null);
		} else {
			if (getText().toString().length() == 0) {
				setCompoundDrawables(null, null, null, null);
			} else {
				setCompoundDrawables(null, null, this.dRight, null);
			}
		}
	}

	private List<Map<String, String>> getData() {
		SearchDBHelper s = new SearchDBHelper(context);
		List<String> usernames = s.getList(context.getPackageName());

		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		for (int i = 0; i < usernames.size(); i++) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("text", usernames.get(i));
			list.add(map);
		}
		return list;
	}

	/**
	 * 初始化弹出列表.
	 * @param types  List<Map<String, String>> 
	 * @param onItemClick OnItemClickListener
	 */
	public void initControls(List<Map<String, String>> types) {
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.core_search_pw, null);
		popupWindow = new PopupWindow(view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		SearchAdapter adapter = new SearchAdapter(context, types, R.layout.core_search_pw_item,
				new String[] { "text"}, new int[] { R.id.item },popupWindow ,this);
		ListView listView = (ListView) view.findViewById(R.id.listview);
		listView.setAdapter(adapter);
		//自适配长、框设置
		popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.pw_bg));
		popupWindow.setOutsideTouchable(true);
		popupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
		popupWindow.update();
		popupWindow.setTouchable(true);
		popupWindow.setFocusable(true);
	}

	private void open() {
		if (show) {
			initControls(getData());

			if (!popupWindow.isShowing()) {
				popupWindow.showAsDropDown(SearchEditText.this, 0, 0);
			}
		}
	}

	public void save(String txt) {
		SearchDBHelper s = new SearchDBHelper(context);
		s.save(context.getPackageName(), txt);
	}

}